/*
 * Copyright (c) 2012 Universidad Antonio de Nebrija.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License v2.0
 * which accompanies this distribution, and is available at
 * http://www.apache.org/licenses/LICENSE-2.0.html
 * 
 * Contributors:
 *    Universidad Antonio de Nebrija - initial API, implementation and documentation
 */
package com.nebrija.riif.model.saver.bufferedwriter;

import java.io.BufferedWriter;
import java.io.IOException;

import com.nebrija.riif.model.AssertOperator;
import com.nebrija.riif.model.AssertValidation;
import com.nebrija.riif.model.saver.AssertValidationSaver;
import com.nebrija.riif.model.saver.RIIFSaverFactory;

/**
 * Implementation of an AssertValidationSaver that writes the object to a BufferedWriter
 * @author asanchep
 *
 */
public class AssertValidationWriterFormatSaver implements AssertValidationSaver {

	/** Assert token from the grammar */
	public static final String ASSERT_TOKEN = "assert";
	
	/** Writer where the information about the object is sent */
	private BufferedWriter out;
	/** Factory used to create other elements of the saver family */
	private RIIFSaverFactory factory;
	
	/**
	 * Creates an AssertValidationWriterFormatSaver
	 * @param bw Writer where the information about the object is sent
	 * @param factory Factory used to create other elements of the saver family
	 */
	public AssertValidationWriterFormatSaver (BufferedWriter bw, RIIFSaverFactory factory){
		out=bw;
		this.factory=factory;
	}
	
	@Override
	public void save(AssertValidation validation) {
		// Write to the buffer writer using the grammar syntax
		try {
			out.append(ASSERT_TOKEN).append(" ").append(validation.getTarget().getName())
						.append("'").append(validation.getTargetProperty()).append(" ")
						.append(AssertOperator.inverseConvert(validation.getOperator())).append(" ")
						.append(validation.getCompareValue()).append(";\n");
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
